<?php
	/*
		Autor: lesthack
		url: http://code.inocorp.org
		class: connection
		class: recordset
		Description: Conection to postgres with php 4x, 5x
	*/
	
	class connection{
		var $bd_host;
		var $bd_usuario; 
		var $bd_password; 
		var $bd_base; 
		var $bd_engie;
		var $link;
		
		//list
		var $list;
		var $pointer = null;
		
		function connection(){
			$this->bd_host = "localhost";
			$this->bd_usuario = "veranos"; 
			$this->bd_base = "veranos"; 
			$this->bd_password = "veranos"; 
			$this->open();
		}
		
		function open(){
			$server = "
				host=$this->bd_host 
				dbname=$this->bd_base 
				user=$this->bd_usuario 
				password=$this->bd_password
			";
			$this->link = pg_pconnect($server) or die("<h2>Conexion fallida</h2>");
		}
		
		function query($query){
			$result = pg_query($query);
			if(!$result) 
				print "<b>".pg_last_error()."</b>";
			return $result;
		}
		
		function close(){
			pg_close($this->link);
		}
		
		function execute($query){
			$result = $this->query($query);
			$this->list = array();
			while($row=pg_fetch_array($result)){
				array_push($this->list,$row);
			}
			if(count($this->list)>0)
				$this->pointer=0;
			return true;
			//pg_free_result();
		}
		
		function executeX($query){
			$result = $this->query($query);
			$myrecordset = new recordset($result);
			return $myrecordset;
		}
		
		function EOF(){
			if($this->pointer>=count($this->list))
				return True;
			else
				return False;
		}
		
		function moveNext(){
			if(!$this->EOF()){
				$this->pointer+=1;
				return $this->getElement();
			}
		}
		
		function moveBack(){
			if($this->pointer>0 && $this->lengthExecute()>0){
				$this->pointer-=1;
				return $this->getElement();
			}
		}
		
		function moveLast(){
			$this->pointer = $this->lengthExecute()-1;
			return $this->getElement();
		}
		
		function moveFirst(){
			$this->pointer = 0;
			return $this->getElement();
		}
		
		function getElement(){
			return $this->list[$this->pointer];
		}
		
		function lengthExecute(){
			return count($this->list);
		}		
	}
	
	class recordset{
		var $list;
		var $pointer = null;
		
		function recordset($result){
			$this->list = array();
			while($row=pg_fetch_array($result)){
				array_push($this->list,$row);
			}
			if(count($this->list)>0)
				$this->pointer=0;
		}
		
		function EOF(){
			if($this->pointer>=count($this->list))
				return True;
			else
				return False;
		}	
		
		function moveNext(){
			if(!$this->EOF()){
				$this->pointer+=1;
				return $this->getElement();
			}
		}
		
		function moveBack(){
			if($this->pointer>0 && $this->lengthExecute()>0){
				$this->pointer-=1;
				return $this->getElement();
			}
		}
		
		function moveLast(){
			$this->pointer = $this->lengthExecute()-1;
			return $this->getElement();
		}
		
		function moveFirst(){
			$this->pointer = 0;
			return $this->getElement();
		}
		
		function getElement(){
			return $this->list[$this->pointer];
		}
		
		function lengthExecute(){
			return count($this->list);
		}
	}	
	
?>	


